from odoo import models, fields, api
from math import ceil
import logging
_logger = logging.getLogger(__name__)


class AccountMoveReport(models.AbstractModel):
    _name = 'report.odoo12_account.report_account_move_1'

    # @api.model
    # def render_html(self, docids, data=None):
    #     report_obj = self.env['report']
    #     report = report_obj._get_report_from_name(
    #         'odoo12_account.report_account_move_1')
    #     docs = self.env[report.model].browse(docids)
    #     docargs = {
    #         'doc_ids': docids,
    #         'doc_model': report.model,
    #         'docs': docs,
    #     }
    #     _logger.info('render_html')
    #     res = report_obj.render(
    #         'odoo12_account.report_account_move_1', docargs)
    #     return res

    @api.model
    def _compute_page(self, doc):
        max_size = int(self.env['ir.config_parameter'].sudo().get_param(
            'odoo12_account.table_diff_size'))
        if max_size == 0:
            max_size = 5
        current_size = len(doc.line_ids)
        page_size = int(ceil(float(current_size) / max_size))
        return page_size

    @api.model
    def _get_lines(self, doc, index):
        max_size = int(self.env['ir.config_parameter'].sudo().get_param(
            'odoo12_account.table_diff_size'))
        if max_size == 0:
            max_size = 5
        i = index*max_size
        return doc.line_ids[i:i+max_size]

    @api.model
    def _get_size_diff(self, doc, index):
        max_size = int(self.env['ir.config_parameter'].sudo().get_param(
            'odoo12_account.table_diff_size'))
        if max_size == 0:
            max_size = 5
        i = index*max_size
        size = len(doc.line_ids[i:i+max_size])
        return max_size - size

    @api.model
    def _display_total(self, doc, index):
        max_size = int(self.env['ir.config_parameter'].sudo().get_param(
            'odoo12_account.table_diff_size'))
        if max_size == 0:
            max_size = 5
        current_size = len(doc.line_ids)
        page_size = int(ceil(float(current_size) / max_size))
        index = index+1
        if index == page_size:
            return True
        else:
            return False

    @api.model
    def _get_report_values(self, docids, data=None):
        docs = self.env['account.move'].browse(docids)
        docargs = {
            'doc_ids': docids,
            'doc_model': docs._name,
            'docs': docs,
            'compute_page': self._compute_page,
            'get_lines': self._get_lines,
            'display_total': self._display_total,
            'get_size_diff': self._get_size_diff,
        }
        return docargs
